SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE procedure dbo.DnnProduct_GetVendorsByName
	@Filter nvarchar(50),
	@PortalID int,
	@PageSize int,
	@PageIndex int
AS

	DECLARE @PageLowerBound int
	DECLARE @PageUpperBound int
	-- Set the page bounds
	SET @PageLowerBound = @PageSize * @PageIndex
	SET @PageUpperBound = @PageLowerBound + @PageSize + 1

	CREATE TABLE #PageIndex 
	(
		IndexID		int IDENTITY (1, 1) NOT NULL,
		VendorId	int
	)

	INSERT INTO #PageIndex (VendorId)
	SELECT VendorId
	FROM dbo.DnnProduct_Vendors
	WHERE ( (VendorName like @Filter + '%') AND ((PortalId = @PortalID) or (@PortalID is null and PortalId is null)) )
	ORDER BY VendorId DESC


	SELECT COUNT(*) as TotalRecords
	FROM #PageIndex


	SELECT dbo.DnnProduct_Vendors.*,
       		'Banners' = ( select count(*) from dbo.DnnProduct_Banners where dbo.DnnProduct_Banners.VendorId = DnnProduct_Vendors.VendorId )
	FROM dbo.DnnProduct_Vendors
	INNER JOIN #PageIndex PageIndex
		ON dbo.DnnProduct_Vendors.VendorId = PageIndex.VendorId
	WHERE ( (PageIndex.IndexID > @PageLowerBound) OR @PageLowerBound is null )	
		AND ( (PageIndex.IndexID < @PageUpperBound) OR @PageUpperBound is null )	
	ORDER BY
		PageIndex.IndexID	

GO
